;---------------------------------------------------------------------------;
; USI control functions
;---------------------------------------------------------------------------;

;.nolist
#include <avr/io.h>	// Include device specific definitions.
;.list

#define SENSOR_PIN PC4
#define SENSOR_DIRECTION DDRC
#define SENSOR_PORT PORTC
#define SENSOR_PORT_IN PINC

timeDelay_us:
	ldi	r21, 4  ;1
1:	dec	r21     ;1+1+1+1
	brne	1b  ;2+2+2+1
	sbiw	r30, 1   ;2
	brne	timeDelay_us ;2
	ret

;---------------------------------------------------------------------------;
; Funktion for init of DS18S20
;---------------------------------------------------------------------------;
.global initSensor
.func initSensor
initSensor:
	sbi _SFR_IO_ADDR(SENSOR_DIRECTION), SENSOR_PIN ;set pin for Sensor like output
	cbi _SFR_IO_ADDR(SENSOR_PORT), SENSOR_PIN ;set this pin in low level
	ldi r30, lo8(480)
	ldi r31, hi8(480)
	rcall timeDelay_us ;delay 480us
	cbi _SFR_IO_ADDR(SENSOR_DIRECTION), SENSOR_PIN ;set pin for Sensor like input
	ldi r30, lo8(80)
	ldi r31, hi8(80)
	rcall timeDelay_us ;delay 80us
	ldi r24, 0 ;preparation of return value like initialization unsuccessful
	sbis _SFR_IO_ADDR(SENSOR_PORT), SENSOR_PIN
	ldi r24, 1
	ldi r30, lo8(400)
	ldi r31, hi8(400)
	rcall timeDelay_us ;delay 80us
	ret
.endfunc

;---------------------------------------------------------------------------;
; Funktion for send one byte to DS18S20
;---------------------------------------------------------------------------;
.global writeSensor
.func writeSensor
writeSensor:
    ldi r18,8     ;store number of bites to send to DS18S20
WRITE_NEXT:
    ror r24
    brcs WRITE_1     ;test of bit for sending, 0 or 1?
WRITE_0:
    sbi _SFR_IO_ADDR(SENSOR_DIRECTION), SENSOR_PIN      ;set pin for Sensor like output
    cbi _SFR_IO_ADDR(SENSOR_PORT), SENSOR_PIN     ;set this pin in low level
	ldi r30, lo8(65)
	ldi r31, hi8(65)
	rcall timeDelay_us ;delay 65us
    cbi _SFR_IO_ADDR(SENSOR_DIRECTION), SENSOR_PIN		;set pin for Sensor like input
    cbi _SFR_IO_ADDR(SENSOR_PORT), SENSOR_PIN		;release wire
	ldi r30, lo8(1)
	ldi r31, hi8(1)
	rcall timeDelay_us ;delay 1us
    rjmp WRITE_NEXT_CHECK
WRITE_1:
    sbi _SFR_IO_ADDR(SENSOR_DIRECTION), SENSOR_PIN      ;set pin for Sensor like output
    cbi _SFR_IO_ADDR(SENSOR_PORT), SENSOR_PIN     ;set this pin in low level
	ldi r30, lo8(1)
	ldi r31, hi8(1)
	rcall timeDelay_us ;delay 1us
    cbi _SFR_IO_ADDR(SENSOR_DIRECTION), SENSOR_PIN      ;set pin for Sensor like output
    cbi _SFR_IO_ADDR(SENSOR_PORT), SENSOR_PIN     ;set this pin in low level
	ldi r30, lo8(65)
	ldi r31, hi8(65)
    rcall timeDelay_us ;delay 65us
WRITE_NEXT_CHECK:
    dec R18
    brne WRITE_NEXT  ;test if all bits were send
	ret
.endfunc

;---------------------------------------------------------------------------;
; Funktion for reading two bytes with temperature value from scratchpad of DS18S20
;---------------------------------------------------------------------------;
.global readSensor
.func readSensor
readSensor:
	in r22, _SFR_IO_ADDR(SREG) ;save SREG
	cli	;global disabling of interrupts
	ldi XL, lo8(TemperatureBuffer)
	ldi XH, hi8(TemperatureBuffer)
	ldi r20, 2 ;16 bits will be read, 2 registers with temperature from scratchpad of BS18S20 -> 8bits x 2
	ldi r19, 0b10000000
READ_NEXT_BYTE:
	ldi r24, 0
	ldi r18, 8 ;16 bits will be read, 2 registers with temperature from scratchpad of BS18S20
READ_BYTE:
    sbi _SFR_IO_ADDR(SENSOR_DIRECTION), SENSOR_PIN      ;set pin for Sensor like output
    cbi _SFR_IO_ADDR(SENSOR_PORT), SENSOR_PIN     ;set this pin in low level
	ldi r30, lo8(1)
	ldi r31, hi8(1)
	rcall timeDelay_us ;delay 1us
	cbi _SFR_IO_ADDR(SENSOR_DIRECTION), SENSOR_PIN      ;set pin for Sensor like input
    cbi _SFR_IO_ADDR(SENSOR_PORT), SENSOR_PIN     ;release wire
	ldi r30, lo8(14)
	ldi r31, hi8(14)
	rcall timeDelay_us ;delay 14us
	lsr r24
	sbic _SFR_IO_ADDR(SENSOR_PORT_IN), SENSOR_PIN
	add r24, r19
	ldi r30, lo8(45)
	ldi r31, hi8(45)
	rcall timeDelay_us ;delay 45us
	dec r18
	brne READ_BYTE;
	st X+, r24
	dec r20
	brne READ_NEXT_BYTE
	out _SFR_IO_ADDR(SREG), r22 ;restore SREG
	ret
.endfunc
